package withdraw

import (
	"context"

	"github.com/jinzhu/copier"

	"gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/dao/model/query"
	modelWithdraw "gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/dao/schema/withdraw"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/rpc"
	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/gorm"
)

type WithdrawChannelModel struct {
	ctx context.Context
	db  *gorm.DB
	logx.Logger
	tx *gorm.DB
}

func NewWithdrawChannelModel(ctx context.Context, db *gorm.DB) *WithdrawChannelModel {
	return &WithdrawChannelModel{
		ctx:    ctx,
		db:     db,
		Logger: logx.WithContext(ctx),
	}
}
func (model *WithdrawChannelModel) getDb() *gorm.DB {
	if model.db != nil {
		return model.db
	}

	return svc.Db
}

func (l *WithdrawChannelModel) WithdrawChannelListPage(in *rpc.WithdrawChannelListPageReq) (*rpc.WithdrawChannelListPageRes, error) {
	ret := &rpc.WithdrawChannelListPageRes{}
	ret.ResHead = &rpc.Head{}
	if in.Page.Page == 0 && in.Page.PageSize == 0 {
		return l.WithdrawChannelList(in)
	}
	if in.Page.Page <= 0 {
		in.Page.Page = 1
	}
	if in.Page.PageSize <= 0 {
		in.Page.PageSize = 10
	}
	g := query.Use(l.getDb()).WithdrawChannel
	h := g.Where()
	if in.GetData().GetName() != "" {
		h = g.Where(g.Name.Eq(in.GetData().GetName()))
	}
	if in.GetData().GetStatus() != 0 {
		h = h.Where(g.Status.Eq(in.GetData().GetStatus()))
	}
	if in.GetData().GetInr() != "" {
		h = h.Where(g.INR.Eq(in.GetData().GetInr()))
	}
	if in.GetData().GetChannelName() != "" {
		h = h.Where(g.INR.Eq(in.GetData().GetChannelName()))
	}
	if h != nil {
		if in.Page.SortOrder == "asc" {
			h = h.Order(g.Sort.Asc())
		} else {
			h = h.Order(g.Sort.Desc())
		}
	} else {
		if in.Page.SortOrder == "asc" {
			h = g.Order(g.Sort.Asc())
		} else {
			h = g.Order(g.Sort.Desc())
		}
	}
	ret.ResHead.Count, _ = h.Count()
	d, _, err := h.FindByPage(int((in.Page.Page-1)*in.Page.PageSize), int(in.Page.PageSize))
	if err != nil {
		return ret, err
	}
	for i, _ := range d {

		channelData := &rpc.WithdrawChannel{}

		copier.Copy(channelData, d[i])
		ret.Data = append(ret.Data, channelData)
	}
	return ret, nil
}
func (l *WithdrawChannelModel) WithdrawChannelList(in *rpc.WithdrawChannelListPageReq) (*rpc.WithdrawChannelListPageRes, error) {
	ret := &rpc.WithdrawChannelListPageRes{}
	g := query.Use(l.getDb()).WithdrawChannel
	var h query.IWithdrawChannelDo
	var d []*modelWithdraw.WithdrawChannel
	var err error
	h = h.Where(g.Status.Eq(in.GetData().GetStatus()))
	d, err = h.Find()
	if err != nil {
		return nil, err
	}
	copier.Copy(ret.Data, d)

	return ret, nil
}
func (l *WithdrawChannelModel) WithdrawChannelDel(in *rpc.WithdrawChannelDelReq) (*rpc.WithdrawChannelDelRes, error) {
	// todo: add your logic here and delete this line

	ret := &rpc.WithdrawChannelDelRes{}
	g := query.Use(l.getDb()).WithdrawChannel
	g.Where(g.ID.Eq(in.Id)).Delete(&modelWithdraw.WithdrawChannel{})
	return ret, nil
}
func (l *WithdrawChannelModel) WithdrawChannelUpdate(in *rpc.WithdrawChannelUpdateReq) (*rpc.WithdrawChannelUpdateRes, error) {
	// todo: add your logic here and delete this line

	ret := &rpc.WithdrawChannelUpdateRes{}
	Withdraw := &modelWithdraw.WithdrawChannel{}
	copier.Copy(Withdraw, in.Data)
	g := query.Use(l.getDb()).WithdrawChannel
	_, err := g.Where(g.ID.Eq(Withdraw.ID)).Updates(Withdraw)
	if err != nil {
		return nil, err
	}
	ret.Data = &rpc.WithdrawChannel{}
	copier.Copy(ret.Data, Withdraw)
	return ret, nil
}
